/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: UserRealm
 * Author:   pc
 * Date:     2019/9/4 10:03
 * Description: 用户数据和Shiro数据交互的桥梁
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.example.rabbitmqdemo.realm;

import com.example.rabbitmqdemo.entity.UserInfo;
import com.example.rabbitmqdemo.service.UserInfoService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 〈功能：〉<br>
 * 〈用户数据和Shiro数据交互的桥梁〉
 * @author liuhaoran
 * @create 2019/9/4 10:03
 * @since 1.0.0
 */
@Component
public class UserRealm extends AuthorizingRealm {

	@Autowired
	UserInfoService UserinfoService;

	/*
   执行授权逻辑
	*/
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		//System.out.println("执行授权逻辑");
		//给资源进行授权
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		//添加资源授权字符串
		info.addStringPermission("index");
		return null;
	}

	/*
	执行认证逻辑
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		//System.out.println("执行认证逻辑");
		UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
		UserInfo user = UserinfoService.findByName(token.getUsername());
		if (user == null) {
			return null;//用户名不存在
		}
		return new SimpleAuthenticationInfo(user.getUserinfoName(), user.getUserinfoPassword(), "");
	}
}